package com.java.service;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.java.entity.Role;

/**
 * 角色管理服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
@Transactional(propagation = Propagation.REQUIRED,readOnly=false, rollbackFor = Exception.class)
public class RoleService extends BaseService{

	/**
	 * 添加角色
	 * @param role
	 */
	public void addRole(Role role){
		this.dao.saveObject(role);
	}
	
	/**
	 * 编辑角色（for dwr）
	 * @param role
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly=false,rollbackFor = Exception.class)
	public void editRole(Role role){
		if(null==role.getId()){
			this.addRole(role);
		}else{
			this.updateRole(role);
		}
	}	
	
	/**
	 * 修改角色
	 * @param role
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly=false,rollbackFor = Exception.class)
	public void updateRole(Role role){
		this.dao.updateObject(role);
	}
	
	/**
	 * 删除角色
	 * @param id
	 */
	public void deleteRole(Long id){
		Role role = this.dao.loadObject(Role.class, id);
		this.dao.deleteObject(role);
	}
	
	public void deleteRoleByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteRole(Long.parseLong(s));
			}			
		}	
	}

	public Role getRoleById(Long id){
		return this.dao.loadObject(Role.class, id);
	}
	
	/**
	 * 修改角色
	 * @param role
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly=false,rollbackFor = Exception.class)
	public void updateRoleAcl(Long id,String acl){
		Role role = this.dao.loadObject(Role.class, id);
		if(null!=role){
			role.setOpAcl(acl);
			this.dao.updateObject(role);
		}
	}
	
}
